package firewing.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Logger;

/**
 * local app to break query logs
 * 
 * @author Shuai Yuan
 * 
 */
public class QueryLogBreaker {

	private static final String INPUT = "data/query-log.txt";

	private static final String OUTPUT_FOLDER = "data/";

	private static final int BREAK_POINT = 100000;

	private static final Logger LOG = Logger.getLogger(QueryLogBreaker.class
			.getName());

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		int counterRecord = 0, counterFile = 0;

		BufferedReader in = new BufferedReader(new FileReader(INPUT));

		BufferedWriter out = new BufferedWriter(new FileWriter(OUTPUT_FOLDER
				+ counterFile + ".txt"));

		for (String s = in.readLine(); s != null; s = in.readLine(), counterRecord++) {
			if (counterRecord >= BREAK_POINT) {
				out = new BufferedWriter(new FileWriter(OUTPUT_FOLDER
						+ ++counterFile + ".txt"));
				counterRecord = 0;

				LOG.info("New file generated");
			}

			try {
				String[] array = s.split("\t");

				out.write(array[1]);
				out.newLine();
			} catch (ArrayIndexOutOfBoundsException e) {
				continue;
			}
		}
	}
}
